//
//  TZHomeController.swift
//  weibo_Swift
//
//  Created by 涂真 on 15/12/15.
//  Copyright © 2015年 涂真. All rights reserved.
//

import UIKit

class TZHomeController: TZBasicController {
    private let ReuseIdentifier = "ReuseIdentifier"
    //MARK: - 属性
    ///微博数据
    var statuses:[TZStatus]?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        if TZUserAccount.isLogin == false{
            return
        }
        //导航栏添加视图
        setUpNavigationItem()
        //注册一个cell
        tableView.registerClass(TZStatusCell.self, forCellReuseIdentifier: ReuseIdentifier)
        // 设置cell预估行高
        tableView.estimatedRowHeight = 200
        
        // 设置cell行高根据约束来自适应
        tableView.rowHeight = UITableViewAutomaticDimension
        
        // 去掉cell的分割线
        tableView.separatorStyle = UITableViewCellSeparatorStyle.None

        //获取微博数据
        TZStatus.loadStatuses { (statuses, error) -> () in
            if error != nil{
                print("微博数据加载失败")
                return
            }
            //TODU:打印微博信息
            //print(statuses)
            //将微博信息赋值给属性
            self.statuses = statuses
            //刷新数据
            self.tableView.reloadData()
        }
        
        
    }
    ///布局头部导航栏
    func setUpNavigationItem(){
        navigationItem.leftBarButtonItem = UIBarButtonItem(imageName: "navigationbar_friendsearch")
        navigationItem.rightBarButtonItem = UIBarButtonItem(imageName: "navigationbar_pop")
        let title = TZUserAccount.loadAccountInfo()?.screen_name ?? "没有加载到用户你昵称"
        let navTitleBtn = TZHomeTitleView(imageName: "navigationbar_arrow_down", title: title)
        navTitleBtn.addTarget(self, action: "navTitleBtnDidClick:", forControlEvents: UIControlEvents.TouchUpInside)
        navigationItem.titleView = navTitleBtn
        
    }
    
    func navTitleBtnDidClick(navTitleBtn:UIButton){
        UIView.animateWithDuration(0.25) { () -> Void in
            navTitleBtn.selected = !navTitleBtn.selected
            navTitleBtn.imageView?.transform = navTitleBtn.selected ? CGAffineTransformMakeRotation(CGFloat(M_PI)-0.001) : CGAffineTransformIdentity
        }
    }
    //MARK: - UITableViewDataSource
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.statuses?.count ?? 0
    }
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier(ReuseIdentifier) as! TZStatusCell
//        let cell = tableView.dequeueReusableCellWithIdentifier(ReuseIdentifier, forIndexPath: indexPath) as! TZStatusCell
        cell.status = statuses?[indexPath.row]
        return cell
    }
}




